<template>
  <div>
    <zh-card title="">
      <zh-form :model="pageData.queryParams" ref="queryFormRef">
<!--        <zh-form-input prop="fieldMapId" label="表格字段映射主键" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.fieldMapId"></zh-form-input>-->
        <zh-form-input prop="tableName" label="表格名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.tableName"></zh-form-input>
<!--        <zh-form-input prop="datasourceId" label="所属数据源" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.datasourceId"></zh-form-input>-->
        <zh-form-input prop="dataSourcename" label="数据源名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.dataSourcename"></zh-form-input>
<!--        <zh-form-input prop="srcTableId" label="源表格表id" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.srcTableId"></zh-form-input>-->
        <zh-form-input prop="srcTableName" label="源表格名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.srcTableName"></zh-form-input>
<!--        <zh-form-input prop="srcColumnId" label="源字段id" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.srcColumnId"></zh-form-input>-->
        <zh-form-input prop="srcColumnType" label="源字段类型" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.srcColumnType"></zh-form-input>
        <zh-form-input prop="srcColumnName" label="源字段名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.srcColumnName"></zh-form-input>
<!--        <zh-form-input prop="mapTableId" label="映射逻辑表格id" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.mapTableId"></zh-form-input>-->
        <zh-form-input prop="mapTableName" label="映射逻辑表格名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.mapTableName"></zh-form-input>
<!--        <zh-form-input prop="mapColumnId" label="映射字段id" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.mapColumnId"></zh-form-input>-->
        <zh-form-input prop="mapColumnType" label="映射字段类型" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.mapColumnType"></zh-form-input>
        <zh-form-input prop="mapColumnName" label="映射字段名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.mapColumnName"></zh-form-input>
        <zh-form-query @queryClick="queryLogicTableFieldMap" :formRef="queryFormRef" :show-advanced="false">
          <el-button @click="showAddDialog">添加</el-button>
        </zh-form-query>
      </zh-form>
    </zh-card>
    <zh-table title="逻辑表字段映射列表" ref="logicTableFieldMapTable"
              size="mini"
              :funcCode="'logicTableFieldMap0001'"
              :columns="pageData.tableColumns"
              highlight-current-row>
      <template #op="scope">
        <el-tooltip content="编辑" placement="top">
          <el-button :icon="Edit" link @click="showEditDialog(scope)"></el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button :icon="Delete" type="danger" link @click="delLogicTableFieldMap(scope.row)"></el-button>
        </el-tooltip>
        <el-tooltip content="详情" placement="top">
          <el-button :icon="Document" link @click="showDetail(scope)"></el-button>
        </el-tooltip>
      </template>
    </zh-table>
    <el-dialog title="添加逻辑表字段映射" v-model="pageData.addLogicTableFieldMapDialogVisible" :width="1200">
      <!--      <zh-form :rules="pageData.formRules">
              <zh-form-input prop="fieldMapId" label="表格字段映射主键" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.fieldMapId"></zh-form-input>
              <zh-form-input prop="tableName" label="表格名称" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.tableName"></zh-form-input>
              <zh-form-input prop="datasourceId" label="所属数据源" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.datasourceId"></zh-form-input>
              <zh-form-input prop="dataSourcename" label="数据源名称" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.dataSourcename"></zh-form-input>
              <zh-form-input prop="srcTableId" label="源表格表id" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.srcTableId"></zh-form-input>
              <zh-form-input prop="srcTableName" label="源表格名称" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.srcTableName"></zh-form-input>
              <zh-form-input prop="srcColumnId" label="源字段id" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.srcColumnId"></zh-form-input>
              <zh-form-input prop="srcColumnType" label="源字段类型" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.srcColumnType"></zh-form-input>
              <zh-form-input prop="srcColumnName" label="源字段名称" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.srcColumnName"></zh-form-input>
              <zh-form-input prop="mapTableId" label="映射逻辑表格id" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.mapTableId"></zh-form-input>
              <zh-form-input prop="mapTableName" label="映射逻辑表格名称" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.mapTableName"></zh-form-input>
              <zh-form-input prop="mapColumnId" label="映射字段id" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.mapColumnId"></zh-form-input>
              <zh-form-input prop="mapColumnType" label="映射字段类型" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.mapColumnType"></zh-form-input>
              <zh-form-input prop="mapColumnName" label="映射字段名称" type="text" :span="8" dataType="common"
                             v-model="pageData.formData.mapColumnName"></zh-form-input>
            </zh-form>-->
      <el-row>
        <el-col :span="12">
          <zh-card title="逻辑表查询">
            <zh-form :model="pageData.logicTableQueryParams" ref="logicTableQueryFormRef">
              <zh-form-input prop="tableName" label="表格名称" type="text" :span="12" dataType="common"
                             v-model="pageData.logicTableQueryParams.tableName"></zh-form-input>
              <zh-form-input prop="columnName" label="字段名称" type="text" :span="12" dataType="common"
                             v-model="pageData.logicTableQueryParams.columnName"></zh-form-input>
              <template v-if="pageData.logicTableQueryAdvanced">
                <zh-form-input prop="tableComment" label="表格备注" type="text" :span="12" dataType="common"
                               v-model="pageData.logicTableQueryParams.tableComment"></zh-form-input>
                <zh-form-input prop="columnComment" label="字段备注" type="text" :span="12" dataType="common"
                               v-model="pageData.logicTableQueryParams.columnComment"></zh-form-input>
              </template>
              <zh-form-query v-model="pageData.logicTableQueryAdvanced"
                             :form-ref="logicTableQueryFormRef"
                             :table-ref="logicTableFieldTableRef"></zh-form-query>
            </zh-form>
          </zh-card>
        </el-col>
        <el-col :span="12">
          <zh-card title="物理表查询">
            <zh-form :model="pageData.physicTableQueryParams" ref="physicTableQueryFormRef">
              <zh-form-input prop="tableName" label="表格名称" type="text" :span="12" dataType="common"
                             v-model="pageData.physicTableQueryParams.tableName"></zh-form-input>
              <zh-form-input prop="columnName" label="字段名称" type="text" :span="12" dataType="common"
                             v-model="pageData.physicTableQueryParams.columnName"></zh-form-input>
              <template v-if="pageData.physicTableQueryAdvanced">
                <zh-form-input prop="tableComment" label="表格备注" type="text" :span="12" dataType="common"
                               v-model="pageData.physicTableQueryParams.tableComment"></zh-form-input>
                <zh-form-input prop="columnComment" label="字段备注" type="text" :span="12" dataType="common"
                               v-model="pageData.physicTableQueryParams.columnComment"></zh-form-input>
              </template>
              <zh-form-query v-model="pageData.physicTableQueryAdvanced"
                             :form-ref="physicTableQueryFormRef"
                             :table-ref="physicTableFieldTableRef"></zh-form-query>
            </zh-form>
          </zh-card>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <zh-table title="逻辑表字段列表" ref="logicTableFieldTableRef" height="300"
                    size="mini"
                    :funcCode="'coTableFieldMeta0009'"
                    :columns="pageData.logicTableColumns"
                    :row-config="{keyField: 'id'}"
                    :radio-config="{checkRowKey: pageData.logicTableField.id, trigger: 'row'}"
                    @radio-change="logicTableRadioChange"
                    highlight-current-row></zh-table>
        </el-col>
        <el-col :span="12">
          <zh-table title="物理表字段列表" ref="physicTableFieldTableRef" height="300"
                    size="mini"
                    :funcCode="'coTableFieldMeta0009'"
                    :columns="pageData.logicTableColumns"
                    :row-config="{keyField: 'id'}"
                    :radio-config="{checkRowKey: pageData.physicTableField.id, trigger: 'row'}"
                    @radio-change="physicTableRadioChange"
                    highlight-current-row></zh-table>
        </el-col>
      </el-row>
<!--      <el-row>
        <el-col :span="24">
          <zh-table title="关联字段列表" ref="mapTableFieldTableRef" height="400"
                    size="mini"
                    :columns="pageData.mapTableColumns"
                    highlight-current-row>
            <template #op="scope">
              <el-tooltip content="解绑" placement="top">
                <el-button :icon="Edit" link @click="showEditDialog(scope)"></el-button>
              </el-tooltip>
            </template>
          </zh-table>
        </el-col>
      </el-row>-->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="pageData.addLogicTableFieldMapDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="mapFields">
            关联
          </el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog title="修改逻辑表字段映射" v-model="pageData.editLogicTableFieldMapDialogVisible" :width="1200">
<!--      <zh-form :rules="pageData.formRules">
        <zh-form-input prop="fieldMapId" label="表格字段映射主键"
                       v-model="pageData.editFormData.fieldMapId"></zh-form-input>
        <zh-form-input prop="tableName" label="表格名称"
                       v-model="pageData.editFormData.tableName"></zh-form-input>
        <zh-form-input prop="datasourceId" label="所属数据源"
                       v-model="pageData.editFormData.datasourceId"></zh-form-input>
        <zh-form-input prop="dataSourcename" label="数据源名称"
                       v-model="pageData.editFormData.dataSourcename"></zh-form-input>
        <zh-form-input prop="srcTableId" label="源表格表id"
                       v-model="pageData.editFormData.srcTableId"></zh-form-input>
        <zh-form-input prop="srcTableName" label="源表格名称"
                       v-model="pageData.editFormData.srcTableName"></zh-form-input>
        <zh-form-input prop="srcColumnId" label="源字段id"
                       v-model="pageData.editFormData.srcColumnId"></zh-form-input>
        <zh-form-input prop="srcColumnType" label="源字段类型"
                       v-model="pageData.editFormData.srcColumnType"></zh-form-input>
        <zh-form-input prop="srcColumnName" label="源字段名称"
                       v-model="pageData.editFormData.srcColumnName"></zh-form-input>
        <zh-form-input prop="mapTableId" label="映射逻辑表格id"
                       v-model="pageData.editFormData.mapTableId"></zh-form-input>
        <zh-form-input prop="mapTableName" label="映射逻辑表格名称"
                       v-model="pageData.editFormData.mapTableName"></zh-form-input>
        <zh-form-input prop="mapColumnId" label="映射字段id"
                       v-model="pageData.editFormData.mapColumnId"></zh-form-input>
        <zh-form-input prop="mapColumnType" label="映射字段类型"
                       v-model="pageData.editFormData.mapColumnType"></zh-form-input>
        <zh-form-input prop="mapColumnName" label="映射字段名称"
                       v-model="pageData.editFormData.mapColumnName"></zh-form-input>
      </zh-form>-->
      <el-row>
        <el-col :span="12">
          <zh-card title="逻辑表查询">
            <zh-form :model="pageData.logicTableQueryParams" ref="logicTableQueryFormRef">
              <zh-form-input prop="tableName" label="表格名称" type="text" :span="12" dataType="common"
                             v-model="pageData.logicTableQueryParams.tableName"></zh-form-input>
              <zh-form-input prop="columnName" label="字段名称" type="text" :span="12" dataType="common"
                             v-model="pageData.logicTableQueryParams.columnName"></zh-form-input>
              <template v-if="pageData.logicTableQueryAdvanced">
                <zh-form-input prop="tableComment" label="表格备注" type="text" :span="12" dataType="common"
                               v-model="pageData.logicTableQueryParams.tableComment"></zh-form-input>
                <zh-form-input prop="columnComment" label="字段备注" type="text" :span="12" dataType="common"
                               v-model="pageData.logicTableQueryParams.columnComment"></zh-form-input>
              </template>
              <zh-form-query v-model="pageData.logicTableQueryAdvanced"
                             :form-ref="logicTableQueryFormRef"
                             :table-ref="logicTableFieldTableRef"></zh-form-query>
            </zh-form>
          </zh-card>
        </el-col>
        <el-col :span="12">
          <zh-card title="物理表查询">
            <zh-form :model="pageData.physicTableQueryParams" ref="physicTableQueryFormRef">
              <zh-form-input prop="tableName" label="表格名称" type="text" :span="12" dataType="common"
                             v-model="pageData.physicTableQueryParams.tableName"></zh-form-input>
              <zh-form-input prop="columnName" label="字段名称" type="text" :span="12" dataType="common"
                             v-model="pageData.physicTableQueryParams.columnName"></zh-form-input>
              <template v-if="pageData.physicTableQueryAdvanced">
                <zh-form-input prop="tableComment" label="表格备注" type="text" :span="12" dataType="common"
                               v-model="pageData.physicTableQueryParams.tableComment"></zh-form-input>
                <zh-form-input prop="columnComment" label="字段备注" type="text" :span="12" dataType="common"
                               v-model="pageData.physicTableQueryParams.columnComment"></zh-form-input>
              </template>
              <zh-form-query v-model="pageData.physicTableQueryAdvanced"
                             :form-ref="physicTableQueryFormRef"
                             :table-ref="physicTableFieldTableRef"></zh-form-query>
            </zh-form>
          </zh-card>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <zh-table title="逻辑表字段列表" ref="logicTableFieldTableRef" height="300"
                    size="mini"
                    :funcCode="'coTableFieldMeta0009'"
                    :columns="pageData.logicTableColumns"
                    :row-config="{keyField: 'id'}"
                    :radio-config="{checkRowKey: pageData.logicTableField.id, trigger: 'row'}"
                    @radio-change="logicTableRadioChange"
                    highlight-current-row></zh-table>
        </el-col>
        <el-col :span="12">
          <zh-table title="物理表字段列表" ref="physicTableFieldTableRef" height="300"
                    size="mini"
                    :funcCode="'coTableFieldMeta0009'"
                    :columns="pageData.logicTableColumns"
                    :row-config="{keyField: 'id'}"
                    :radio-config="{checkRowKey: pageData.physicTableField.id, trigger: 'row'}"
                    @radio-change="physicTableRadioChange"
                    highlight-current-row></zh-table>
        </el-col>
      </el-row>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="pageData.editLogicTableFieldMapDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="mapFields">
            关联
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup name="logicTableFieldMap">
import {Delete, Edit, Document} from '@element-plus/icons-vue'
import {ElMessageBox} from "element-plus";
// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
  init();
})

// ------------------定义子组件--------------------------
const logicTableFieldMapTable = ref();
const queryFormRef = ref();
const logicTableQueryFormRef = ref();
const logicTableFieldTableRef = ref();
const physicTableFieldTableRef = ref();
const physicTableQueryFormRef = ref();
// ------------------变量区------------------------------
const pageData = reactive({
  addLogicTableFieldMapDialogVisible: false,
  editLogicTableFieldMapDialogVisible: false,
  detailDialog: false,
  tableColumns: [
    {type: 'seq', width: 60, fixed: 'left', align: 'center', title: '序号'},
    // {field: 'fieldMapId', title: '表格字段映射主键', visible: true, align: 'center', width: 100},
    {field: 'tableName', title: '表格名称', visible: true, align: 'center', width: 150},
    // {field: 'datasourceId', title: '所属数据源', visible: true, align: 'center', width: 100},
    {field: 'dataSourcename', title: '数据源名称', visible: true, align: 'center', width: 100},
    // {field: 'mapTableId', title: '映射逻辑表格id', visible: true, align: 'center', width: 100},
    {field: 'mapTableName', title: '映射逻辑表格名称', visible: true, align: 'center', width: 150},
    // {field: 'mapColumnId', title: '映射字段id', visible: true, align: 'center', width: 100},
    {field: 'mapColumnName', title: '映射字段名称', visible: true, align: 'center', width: 100},
    {field: 'mapColumnType', title: '映射字段类型', visible: true, align: 'center', width: 100},
    // {field: 'srcTableId', title: '源表格表id', visible: true, align: 'center', width: 100},
    {field: 'srcTableName', title: '源表格名称', visible: true, align: 'center', width: 150},
    // {field: 'srcColumnId', title: '源字段id', visible: true, align: 'center', width: 100},
    {field: 'srcColumnName', title: '源字段名称', visible: true, align: 'center', width: 100},
    {field: 'srcColumnType', title: '源字段类型', visible: true, align: 'center', width: 100},
    {field: 'op', title: '操作', visible: true, align: 'center', width: 140, fixed: 'right', slots: {default: 'op'}},
  ],
  queryParams: {
    fieldMapId: '',
    tableName: '',
    datasourceId: '',
    dataSourcename: '',
    srcTableId: '',
    srcTableName: '',
    srcColumnId: '',
    srcColumnType: '',
    srcColumnName: '',
    mapTableId: '',
    mapTableName: '',
    mapColumnId: '',
    mapColumnType: '',
    mapColumnName: ''
  },
  formData: {
    fieldMapId: '',
    tableName: '',
    datasourceId: '',
    dataSourcename: '',
    srcTableId: '',
    srcTableName: '',
    srcColumnId: '',
    srcColumnType: '',
    srcColumnName: '',
    mapTableId: '',
    mapTableName: '',
    mapColumnId: '',
    mapColumnType: '',
    mapColumnName: ''
  },
  editFormData: {
    fieldMapId: '',
    tableName: '',
    datasourceId: '',
    dataSourcename: '',
    srcTableId: '',
    srcTableName: '',
    srcColumnId: '',
    srcColumnType: '',
    srcColumnName: '',
    mapTableId: '',
    mapTableName: '',
    mapColumnId: '',
    mapColumnType: '',
    mapColumnName: ''
  },
  formRules: {},
  dictFields: '',
  dictData: [],
  logicTableField: {
    id: '',
    tableId: '',
    columnName: '',
    columnComment: '',
    dataType: '',
  },
  physicTableField: {
    id: '',
    tableId: '',
    columnName: '',
    columnComment: '',
    dataType: '',
  },
  logicTableColumns: [
    {type: 'radio', width: 40, fixed: 'left', align: 'center',},
    {type: 'seq', width: 40, fixed: 'left', align: 'center', title: '序号'},
    {field: 'id', title: '字段id', visible: false, align: 'center', width: 100},
    {field: 'tableId', title: '表格id', visible: false, align: 'center', width: 100},
    {field: 'tableName', title: '表格名称', visible: true, align: 'center', width: 100},
    // {field: 'tableComment', title: '表格注释', visible: true, align: 'center', width: 100},
    {field: 'columnName', title: '字段名称', visible: true, align: 'center', width: 100},
    {field: 'columnComment', title: '字段中文', visible: true, align: 'center', width: 100},
    {field: 'dataType', title: '字段类型', visible: true, align: 'center', width: 100},
  ],
  mapTableColumns: [
    {type: 'seq', width: 60, fixed: 'left', align: 'center', title: '序号'},
    {field: 'logicTableFieldId', title: '逻辑表格字段id', visible: false, align: 'center', width: 100},
    {field: 'logicTableId', title: '逻辑表格id', visible: false, align: 'center', width: 100},
    {field: 'logicTableColumnName', title: '逻辑表格字段名称', visible: true, align: 'center', width: 100},
    {field: 'logicTableColumnComment', title: '逻辑表格字段中文', visible: true, align: 'center', width: 100},
    {field: 'logicTableDataType', title: '逻辑表格字段类型', visible: true, align: 'center', width: 100},
    {field: 'physicTableFieldId', title: '物理表格字段id', visible: false, align: 'center', width: 100},
    {field: 'physicTableId', title: '物理表格id', visible: false, align: 'center', width: 100},
    {field: 'physicTableColumnName', title: '物理表格字段名称', visible: true, align: 'center', width: 100},
    {field: 'physicTableColumnComment', title: '物理表格字段中文', visible: true, align: 'center', width: 100},
    {field: 'physicTableDataType', title: '物理表格字段类型', visible: true, align: 'center', width: 100},
    {field: 'op', title: '操作', visible: true, align: 'center', width: 140, fixed: 'right', slots: {default: 'op'}},
  ],
  logicTableQueryAdvanced: false,
  logicTableQueryParams: {
    tableId: '',
    tableType: '2',
    columnName: '',
    columnComment: '',
    tableName: '',
    tableComment: '',
  },
  physicTableQueryAdvanced: false,
  physicTableQueryParams: {
    tableId: '',
    tableType: '1',
    columnName: '',
    columnComment: '',
    tableName: '',
    tableComment: '',
  }
});
// ------------------方法区------------------------------
const init = () => {
  getDict();
  queryLogicTableFieldMap();
}
// 查询数据字典信息
const getDict = () => {
  let params = {
    dictFields: pageData.dictFields,
  };
  $api.common.getBatchDict.post(params).then((res: any) => {
    pageData.dictData = res.result;
  });
};
const queryLogicTableFieldMap = () => {
  logicTableFieldMapTable.value.query(pageData.queryParams);
}
const showAddDialog = () => {
  pageData.addLogicTableFieldMapDialogVisible = true;
  pageData.formData = {
    fieldMapId: '',
    tableName: '',
    datasourceId: '',
    dataSourcename: '',
    srcTableId: '',
    srcTableName: '',
    srcColumnId: '',
    srcColumnType: '',
    srcColumnName: '',
    mapTableId: '',
    mapTableName: '',
    mapColumnId: '',
    mapColumnType: '',
    mapColumnName: ''
  }
}
const showEditDialog = (scope) => {
  pageData.editLogicTableFieldMapDialogVisible = true;
  pageData.editFormData = {...scope.row};
  pageData.physicTableField.id = scope.row.srcColumnId;
  pageData.physicTableField.tableId = scope.row.srcTableId;
  pageData.physicTableField.tableName = scope.row.srcTableName;
  pageData.physicTableField.dataType = scope.row.srcColumnType;
  pageData.physicTableField.columnName = scope.row.srcColumnName;
  pageData.logicTableField.id = scope.row.mapColumnId;
  pageData.logicTableField.tableId = scope.row.mapTableId;
  pageData.logicTableField.tableName = scope.row.mapTableName;
  pageData.logicTableField.dataType = scope.row.mapColumnType;
  pageData.logicTableField.columnName = scope.row.mapColumnName;
  Object.assign(pageData.logicTableQueryParams, {tableId: '', tableType: '2', columnName: scope.row.mapColumnName, columnComment: '', tableName: scope.row.mapTableName, tableComment: '',});
  Object.assign(pageData.physicTableQueryParams, {tableId: '', tableType: '1', columnName: scope.row.srcColumnName, columnComment: '', tableName: scope.row.srcTableName, tableComment: '',});
  nextTick(() => {
    logicTableFieldTableRef.value.query(pageData.logicTableQueryParams).then(() => {
      let radioRow = logicTableFieldTableRef.value.tableData.find(item => item.id == pageData.logicTableField.id);
      logicTableFieldTableRef.value.setRadioRow(radioRow);
    });
    physicTableFieldTableRef.value.query(pageData.physicTableQueryParams).then(() => {
      let radioRow = physicTableFieldTableRef.value.tableData.find(item => item.id == pageData.physicTableField.id);
      physicTableFieldTableRef.value.setRadioRow(radioRow);
    });
  });
}
const addLogicTableFieldMap = () => {
  $api.common.send.post('logicTableFieldMap0004', pageData.formData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('添加逻辑表字段映射成功');
      pageData.addLogicTableFieldMapDialogVisible = false;
      queryLogicTableFieldMap();
    }
  })
}
const editLogicTableFieldMap = () => {
  $api.common.send.post('logicTableFieldMap0005', pageData.editFormData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('修改逻辑表字段映射成功');
      pageData.editLogicTableFieldMapDialogVisible = false;
      queryLogicTableFieldMap();
    }
  })
}
const delLogicTableFieldMap = ({fieldMapId}) => {
  ElMessageBox.confirm('确定删除该逻辑表字段映射?').then(() => {
    $api.common.send.post('logicTableFieldMap0006', {fieldMapId: fieldMapId}).then(res => {
      if (res.code == 0) {
        ElMessageBox.alert('删除逻辑表字段映射成功');
        queryLogicTableFieldMap();
      }
    })
  })
}

const logicTableRadioChange = ({newValue}) => {
  pageData.logicTableField = newValue;
}

const physicTableRadioChange = ({newValue}) => {
  pageData.physicTableField = newValue;
}

const mapFields = () => {
  if (!pageData.logicTableField || !pageData.logicTableField.id
      || !pageData.physicTableField || !pageData.physicTableField.id) {
    ElMessageBox.alert('请先选择要关联的逻辑表和物理表字段');
    return;
  }
  if (pageData.addLogicTableFieldMapDialogVisible) {
    ElMessageBox.confirm(`确定关联${pageData.logicTableField.columnName}和${pageData.physicTableField.columnName}?`).then(() => {
      pageData.formData.tableName = pageData.logicTableField.tableName;
      // pageData.formData.datasourceId = pageData.logicTableField.tableName;
      // pageData.formData.dataSourcename = pageData.logicTableField.tableName;
      pageData.formData.srcTableId = pageData.physicTableField.tableId;
      pageData.formData.srcTableName = pageData.physicTableField.tableName;
      pageData.formData.srcColumnId = pageData.physicTableField.id;
      pageData.formData.srcColumnType = pageData.physicTableField.dataType;
      pageData.formData.srcColumnName = pageData.physicTableField.columnName;
      pageData.formData.mapTableId = pageData.logicTableField.tableId;
      pageData.formData.mapTableName = pageData.logicTableField.tableName;
      pageData.formData.mapColumnId = pageData.logicTableField.id;
      pageData.formData.mapColumnType = pageData.logicTableField.dataType;
      pageData.formData.mapColumnName = pageData.logicTableField.columnName;
      addLogicTableFieldMap();
    });
  }
  if (pageData.editLogicTableFieldMapDialogVisible) {
    ElMessageBox.confirm(`确定关联${pageData.logicTableField.columnName}和${pageData.physicTableField.columnName}?`).then(() => {
      pageData.editFormData.tableName = pageData.logicTableField.tableName;
      pageData.editFormData.srcTableId = pageData.physicTableField.tableId;
      pageData.editFormData.srcTableName = pageData.physicTableField.tableName;
      pageData.editFormData.srcColumnId = pageData.physicTableField.id;
      pageData.editFormData.srcColumnType = pageData.physicTableField.dataType;
      pageData.editFormData.srcColumnName = pageData.physicTableField.columnName;
      pageData.editFormData.mapTableId = pageData.logicTableField.tableId;
      pageData.editFormData.mapTableName = pageData.logicTableField.tableName;
      pageData.editFormData.mapColumnId = pageData.logicTableField.id;
      pageData.editFormData.mapColumnType = pageData.logicTableField.dataType;
      pageData.editFormData.mapColumnName = pageData.logicTableField.columnName;
      editLogicTableFieldMap();
    });
  }
}

</script>
<style lang="scss" scoped></style>
